<template>
    <div class="home-box">
        <header>
            <h3>潮服购</h3>
            <van-icon name="like" class="love"/>
            <van-search v-model="search" @search="seaEvt(search)" @click-left-icon="seaEvt(search)" placeholder="年末折扣季" />
            <van-icon name="photograph" class="camera" @click="showToast('功能开发中')"/>
        </header>
        <main ref="node">
            <div class="banner">
                <van-swipe :autoplay="3000" lazy-render>
                    <van-swipe-item v-for="it in images" :key="it">
                        <img :src="it" />
                    </van-swipe-item>
                </van-swipe>
                <div class="welfare">
                    <span class="iconfont icon-diqiu">全场包邮</span>
                    <span class="iconfont icon-a-jiaofeijilu1x">十亿补贴回馈用户</span>
                    <span class="iconfont icon-kuaidi">三天无理由退换</span>
                </div>
            </div>
            <div class="hotTitle">
                <h3>热门专区 | 本月 100+ 爆款</h3>
                <p>为您精挑细选的Dior、Louis Vuitton...</p>
            </div>
            <div class="goods">
                <div class="hotGoods" v-for="it in hotList" :key="it.id" @click="detEvt(it.id)">
                    <img :src="it.img">
                    <h5>{{ it.brand }}</h5>
                    <p>{{ it.title }}</p>
                    <div class="price">
                        <span class="l">￥{{ it.oldPrice }}</span>
                        <span class="m">￥{{ it.price }}</span>
                        <span class="r">10亿补贴</span>
                    </div>
                </div>
            </div>
            <div class="clearance">
                <h4>断码清仓</h4>
                <span class="iconfont icon-sanjiaoxing"></span>
                <img src="../../images/home-clear.png">
            </div>
            <div class="clearance">
                <h4>
                    猜你喜欢
                    <span class="iconfont icon-xingxing"></span>
                </h4>
                <div class="goods">
                    <div class="hotGoods" v-for="it in goodsList" :key="it.id" @click="detEvt(it.id)">
                        <img :src="it.img">
                        <h5>{{ it.brand }}</h5>
                        <p>{{ it.title }}</p>
                        <div class="price">
                            <span class="l">￥{{ it.oldPrice }}</span>
                            <span class="m">￥{{ it.price }}</span>
                            <span class="r">10亿补贴</span>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</template>

<script lang="ts" setup>
import { showToast } from 'vant';
import banner from '../../images/banner.png'
import banner2 from '../../images/banner2.png'
import banner3 from '../../images/banner3.png'
import { onActivated, Ref, ref, watch } from 'vue';
import { getGoodsApi, goodsDetailsApi } from '../../apis/goodsApi';
import router from '../../router';

const images = [
    banner, banner2, banner3
]
const search:Ref<string> = ref('')

/** 热门商品 */
const hotList:Ref<any> = ref([])
const goodsList:Ref<any> = ref([])
hotGoods('男装')
async function hotGoods(type: string) {
    let res = await getGoodsApi(type)
    goodsList.value = res.data
    hotList.value = res.data.slice(10, 12)
}

/** 搜索 */
function seaEvt(val: string) {
    sessionStorage.setItem('search', val)
    router.push('/search')
}

/** 详情页 */
async function detEvt(id: string) {
    console.log(goodsList.value)
    let res = await goodsDetailsApi(id)
    sessionStorage.setItem('goodsId', res.data.id)
    sessionStorage.setItem('scrollTop', node.value.scrollTop)
    router.push('/details')
}

/** 缓存 */
const node = ref()
watch(
    () => node.value,
    function() {
        sessionStorage.setItem('scrollTop', node.value.scrollTop)
    }
)
onActivated(() => {
    node.value.scrollTop = sessionStorage.getItem('scrollTop')
    search.value = ''
})


</script>

<style lang="less" scoped>
@import '../../style/common.less';

.home-box {
    width: 100vw;
    height: 100%;
    text-align: center;

    header {
        width: 100%;
        padding-top: 20px;

        h3 {
            letter-spacing: 2px;
        }

        .love {
            font-size: 24px;
            position: fixed;
            right: 10px;
            top: 24px;
        }

        .van-search {
            --van-search-input-height: 50px;
            margin-top: 10px;
        }

        .camera {
            font-size: 28px;
            position: fixed;
            right: 20px;
            top: 80px;
        }
    }

    main {
        width: 100vw;
        height: calc(100% - 131px);
        background-color: @b5;
        overflow-x: hidden;
        &::-webkit-scrollbar {
            display: none;
        }

        .banner {
            width: 100%;
            height: 360px;
            background-color: @g1;

            .van-swipe {
                width: 100%;
                height: 300px;
                --van-swipe-indicator-active-background: @g1;

                img {
                    width: 100%;
                    height: 100%;
                }
            }

            .welfare {
                width: 100%;
                height: 60px;
                display: flex;
                justify-content: space-around;
                align-items: center;

                span {
                    font-size: 12px;
                }

                .iconfont::before {
                    font-size: 22px;
                    vertical-align: middle;
                    margin-right: 4px;
                }

                .icon-a-jiaofeijilu1x::before {
                    font-size: 30px;    
                }

                .icon-kuaidi::before {
                    font-size: 34px;    
                }
            }
        }

        .hotTitle {
            width: 100vw;
            height: 140px;
            background-color: @g1;
            margin-top: 4px;
            margin-bottom: 10px;

            h3 {
                line-height: 80px;
            }
        }

        .goods {
            width: 100%;
            display: flex;
            justify-content: space-around;
            flex-wrap: wrap;

            .hotGoods {
                width: 180px;
                height: 310px;
                background-color: @g1;
                border-radius: 20px;
                margin-bottom: 10px;

                img {
                    width: 100%;
                    height: 180px;
                    border-radius: 20px 20px 0 0;
                }

                h5 {
                    line-height: 40px;
                }

                p {
                    font-size: 16px;
                    text-align: left;
                    display: -webkit-box;
                    -webkit-line-clamp: 2;
                    -webkit-box-orient: vertical;
                    overflow: hidden;
                    padding-left: 5px;
                }

                .price {
                    width: 100%;
                    display: flex;
                    justify-content: space-around;
                    font-size: 14px;
                    margin-top: 18px;

                    .l {
                        text-decoration: line-through;
                        color: @c3;
                    }

                    .r {
                        background-color: red;
                        color: @g1;
                        border-radius: 20px;
                    }
                }
            }
        }

        .clearance {
            width: 100%;

            h4 {
                margin-top: 20px;
                margin-bottom: 10px;
            }

            .icon-sanjiaoxing {
                font-size: 30px;
            }

            .icon-xingxing {
                font-size: 30px;
                color: #f1eb29;
                vertical-align:super;
            }
        }
    }
}

::v-deep .van-search__field {
    font-size: 18px;
}

::v-deep .van-icon-clear {
    opacity: .0;
}
</style>